//package org.example.security;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.stereotype.Component;
//
//@Component
//@Slf4j
//public class UserAuthenticationProvider implements AuthenticationProvider {
//
//    @Autowired
//    private UserDetailsService userService;
//
//    public PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
//
//    /**
//     * 自己实现认证过程
//     *
//     * @param authentication
//     * @return
//     * @throws AuthenticationException
//     */
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        // 从Authentication 对象中获取用户名和密码
//        String username = authentication.getName();
//        String password = authentication.getCredentials().toString();
//
//        UserDetails user = userService.loadUserByUsername(username);
//
//        // if (password.equals(user.getPassword())) {
//        if (this.passwordEncoder().matches(password, user.getPassword())) {
//            // 密码匹配成功
//            log.info("Access Success: " + user);
//            return new UsernamePasswordAuthenticationToken(username, password, user.getAuthorities());
//        } else {
//            // 密码匹配失败
//            log.error("Access Denied: The username or password is wrong!");
//            throw new BadCredentialsException("The username or password is wrong!");
//        }
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return authentication.equals(UsernamePasswordAuthenticationToken.class);
//    }
//}